package com.example.tse.rbhttpservice.service.member;


import android.content.Context;

import com.example.tse.rbhttpservice.service.HttpService;
import com.example.tse.rbhttpservice.service.RemoteServiceListenerT;
import com.example.tse.rbhttpservice.service.ResponseServiceListenerT;
import com.example.tse.rbhttpservice.service.RudderResponse;
import com.example.tse.rbhttpservice.service.member.model.MemberHistoryGetList;
import com.example.tse.rbhttpservice.service.member.model.MemberHistoryGetListRequest;
import com.example.tse.rbhttpservice.service.member.model.MemberHistoryGetListResponse;
import com.example.tse.rbhttpservice.service.member.model.MemberHistoryRemoveRequest;
import com.example.tse.rbhttpservice.service.member.model.MemberHistoryRemoveResponse;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.kymjs.kjframe.http.HttpParams;

import java.lang.reflect.Type;
import java.util.List;


/**
 * 
 * @author wliu
 *    edi  tse
 */
public class MemberHistoryServiceImpl implements MemberHistoryService {
	private Context context;
	private static Gson gson = new Gson();
	
	
	public MemberHistoryServiceImpl(Context context) {
		this.context = context;
	}
	/**
	 * 获取当前用户浏览商品历史纪录
	 * @param request
	 * @param listener
	 * @return MemberHistoryGetListResponse
	 */		
	 
	    public void getList(MemberHistoryGetListRequest request, final RemoteServiceListenerT<MemberHistoryGetListResponse> listener) {

        final HttpParams params = new HttpParams();
        


        final String url = "member/history/getList";


        
        /**
        history
        getList
        GetList
        获取当前用户浏览商品历史纪录
        GET
        MemberHistory
        MemberHistoryGetList
        member/history/getList
        array
        */
	
           final Type type = new TypeToken<List<MemberHistoryGetList>>() {
        }.getType();



            new HttpService().getList(context, url, params, MemberHistoryGetListResponse.class, new ResponseServiceListenerT<RudderResponse>() {

             @Override
             public void onPreStart() {
                 super.onPreStart();
                 listener.PreStart();
             }

             @Override
             public void ok(RudderResponse rudderResponse) {
                 super.ok(rudderResponse);
                 List<MemberHistoryGetList> items = gson.fromJson(rudderResponse.getBody(), type);
                 MemberHistoryGetListResponse responce = new MemberHistoryGetListResponse();
                 responce.setGetList(items);
                 listener.ok(responce);
             }

             @Override
             public void okData(RudderResponse rudderResponse) {
                 super.okData(rudderResponse);

                 if (rudderResponse == null) {
                     listener.okData(new MemberHistoryGetListResponse());
                 } else {
                     List<MemberHistoryGetList> items = gson.fromJson(rudderResponse.getBody(), type);
                     MemberHistoryGetListResponse responce = new MemberHistoryGetListResponse();
                     responce.setGetList(items);
                     listener.okData(rudderResponse == null ? new MemberHistoryGetListResponse() : responce);
                 }
             }

             @Override
             public void okData(RudderResponse rudderResponse, int fromWhere) {
                 super.okData(rudderResponse, fromWhere);


                 if (rudderResponse == null) {
                     listener.okData(new MemberHistoryGetListResponse(), fromWhere);
                 } else {
                     List<MemberHistoryGetList> items = gson.fromJson(rudderResponse.getBody(), type);
                     MemberHistoryGetListResponse responce = new MemberHistoryGetListResponse();
                     responce.setGetList(items);
                     listener.okData(rudderResponse == null ? new MemberHistoryGetListResponse() : responce, fromWhere);
                 }


             }

             @Override
             public void onFailure(int errorNo, String strMsg) {
                 super.onFailure(errorNo, strMsg);
                 listener.failue(errorNo, strMsg);
             }

             @Override
             public void onFinish() {
                 super.onFinish();
                 listener.Finish();
             }
         });
    }            
        
        

   
	
	
	/**
	 * 用户删除个人的浏览历史记录
	 * @param request
	 * @param listener
	 * @return MemberHistoryRemoveResponse
	 */		
	 
	    public void remove(MemberHistoryRemoveRequest request, final RemoteServiceListenerT<MemberHistoryRemoveResponse> listener) {

        final HttpParams params = new HttpParams();
        

            params.put("goodsId",request.getGoodsId());

        final String url = "member/history/remove";


            new HttpService().post(context, url, params, MemberHistoryRemoveResponse.class, new ResponseServiceListenerT<RudderResponse>() {

            @Override
            public void onPreStart() {
                super.onPreStart();
                listener.PreStart();
            }

            public void ok(RudderResponse rudderResponse) {
                super.ok(rudderResponse);
                listener.ok((MemberHistoryRemoveResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse) {
                super.okData(rudderResponse);
                listener.okData(rudderResponse == null ? new MemberHistoryRemoveResponse() : (MemberHistoryRemoveResponse) rudderResponse);
            }

            @Override
            public void okData(RudderResponse rudderResponse, int fromWhere) {
                super.okData(rudderResponse, fromWhere);
                listener.okData(rudderResponse == null ? new MemberHistoryRemoveResponse() : (MemberHistoryRemoveResponse) rudderResponse, fromWhere);
            }

            @Override
            public void onFailure(int errorNo, String strMsg) {
                super.onFailure(errorNo, strMsg);
                listener.failue(errorNo, strMsg);
            }

            @Override
            public void onFinish() {
                super.onFinish();
                listener.Finish();
            }
        });
    }

   
	
	
}